package com.song.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.song.oa.mapper.AdminMapper;
import com.song.oa.pojo.Admin;
import com.song.oa.service.AdminService;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @Author 宋伟宁
 * @Date 2024/3/20
 * @Version 1.0
 **/
@Component
public class LoginUserService implements UserDetailsService {

    @Resource
    private AdminMapper adminMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Admin admin = this.adminMapper.selectOne(new LambdaQueryWrapper<Admin>()
                .eq(Admin::getUsername, username).eq(Admin::getEnabled, 1));
        if(Objects.isNull(admin)){
            throw new UsernameNotFoundException("账号或密码错误！");
        }
        //获取权限
        return admin;

    }
}
